﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.Design;

namespace TA.Win.Utils.Design
{
	/// <summary>
	/// Allows editing an object, containing a set of properties, in a Windows form.
	/// </summary>
	public class PropertyDialog : Form
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public PropertyDialog()
		{
			this.Size = new Size(220, 470);
			this.FormBorderStyle = FormBorderStyle.FixedDialog;

			pg = new PropertyGrid();
			pg.Location = new System.Drawing.Point(5, 5);
			pg.Size = new System.Drawing.Size(200, 400);
			pg.PropertyTabs.AddTabType(typeof(EventsTab));
			this.Controls.Add(pg);

			this.btnOk = new System.Windows.Forms.Button
			{
				Text = "OK",
				Location = new Point(5, 410),
			};
			this.btnOk.Click += new EventHandler(btnOk_Click);
			this.Controls.Add(this.btnOk);

			this.btnCancel = new Button
			{
				Text = "OK",
				Location = new System.Drawing.Point(5, 410),
			};
			this.btnCancel.Click += new EventHandler(btnCancel_Click);
			this.Controls.Add(this.btnCancel);
		}
		#endregion
		#region PROPS
		/// <summary>
		/// Gets or sets the properties object.
		/// </summary>
		public object Value
		{
			get
			{
				return this.pg.SelectedObject;
			}
			set
			{
				if (!this._textChanged)
					this.Text = value.GetType().Name;

				this.pg.SelectedObject = value;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		PropertyGrid pg;
		Button btnOk, btnCancel;
		bool _textChanged = false;
		#endregion
		#region METHODS
		#region EVENTS
		/// <summary>
		/// Raises the <see cref="System.Windows.Forms.Control.TextChanged"/> event.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnTextChanged(EventArgs e)
		{
			base.OnTextChanged(e);

			this._textChanged = true;
		}
		#endregion
		#region HANDLERS
		void btnOk_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
		}
		void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}
		#endregion
		#endregion
		#endregion
	}
}
